// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-22 16:27:56
 * @LastEditors: qiulin_JXGM ql1746365080@gmail.com
 * @LastEditTime: 2024-12-19 16:47:01
 * @FilePath: \SGFEM\Import\BDFImport\test\Load\PLOAD4ParserTest.cpp
 */

#include "Import/BDFImport/BulkData/Load/PLOAD4Parser.h"

#include <set>

#include "gtest/gtest.h"

#include "DataStructure/Common/Constant.h"
#include "DataStructure/Input/Load/PLOAD4Data.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/IPLOAD4Service.h"

#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;

using SG::DataStructure::Common::TOLERANCE;
using namespace SG::DataStructure::FEM;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class PLOAD4ParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (PLOAD4ParserTest, ParseEntry1)
{
    std::vector<std::string> Entry = { "PLOAD4         2     441    -90.                             577     579+0000A9B",
                                       "+0000A9B              0.      0.      1." };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePLOAD4 (DB_SERVICEFACTORY, dividedEntryInfo);
    std::vector<SG::DataStructure::FEM::PLOAD4Data> Ploads;

    SG::DataStructure::FEM::PLOAD4Data data;

    DB_SERVICEFACTORY->get<IPLOAD4Service> ()->FindBySId (2, Ploads);

    data = Ploads[0];

    ASSERT_TRUE (data.m_sId == 2);
    ASSERT_TRUE (data.m_eId == 441);
    ASSERT_TRUE (data.m_G1 == 577);
    ASSERT_TRUE (data.m_G34 == 579);
    ASSERT_TRUE (data.m_cId == 0);
    ASSERT_NEAR (data.m_P[0], -90.0, TOLERANCE);
    ASSERT_NEAR (data.m_P[1], -90.0, TOLERANCE);
    ASSERT_NEAR (data.m_P[2], -90.0, TOLERANCE);
    ASSERT_NEAR (data.m_N[0], 0.0, TOLERANCE);
    ASSERT_NEAR (data.m_N[1], 0.0, TOLERANCE);
    ASSERT_NEAR (data.m_N[2], 1.0, TOLERANCE);
    char m_sorl[8] = "SURF";
    char m_ldir[8] = "NORM";
    ASSERT_STREQ (data.m_sorl, m_sorl);
    ASSERT_STREQ (data.m_ldir, m_ldir);
}

TEST_F (PLOAD4ParserTest, ParseEntry2)
{
    // PLOAD4 SID EID P1 P2 P3 P4 G1 G3/G4
    //                                  $1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567
    //
    std::vector<std::string> Entry = { "PLOAD4   5       1       1.                              THRU    40" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePLOAD4 (DB_SERVICEFACTORY, dividedEntryInfo);
    std::vector<SG::DataStructure::FEM::PLOAD4Data> Ploads;

    SG::DataStructure::FEM::PLOAD4Data data;

    DB_SERVICEFACTORY->get<IPLOAD4Service> ()->FindBySId (5, Ploads);

    data = Ploads[0];
    ASSERT_TRUE (Ploads.size () == 40);

    std::set<SG::DataStructure::Common::Id> eidSet;
    for (size_t i = 0; i < Ploads.size (); i++)
    {
        if (Ploads[i].m_eId <= 40 && Ploads[i].m_eId >= 1)
        {
            eidSet.insert (Ploads[i].m_eId);
        }
    }
    ASSERT_TRUE (eidSet.size () == 40);

    ASSERT_TRUE (data.m_sId == 5);
    ASSERT_TRUE (data.m_G1 == 0);
    ASSERT_TRUE (data.m_G34 == 0);
    ASSERT_TRUE (data.m_cId == 0);
    ASSERT_NEAR (data.m_P[0], 1.0, TOLERANCE);
    ASSERT_NEAR (data.m_P[1], 1.0, TOLERANCE);
    ASSERT_NEAR (data.m_P[2], 1.0, TOLERANCE);
    ASSERT_NEAR (data.m_N[0], 0.0, TOLERANCE);
    ASSERT_NEAR (data.m_N[1], 0.0, TOLERANCE);
    ASSERT_NEAR (data.m_N[2], 0.0, TOLERANCE);
    char m_sorl[8] = "SURF";
    char m_ldir[8] = "NORM";
    ASSERT_STREQ (data.m_sorl, m_sorl);
    ASSERT_STREQ (data.m_ldir, m_ldir);
}

TEST_F (PLOAD4ParserTest, ParseEntry3Long)
{
    std::vector<std::string> Entry = {
        "PLOAD4*  4               14016           9.9999997-5",
        "*                                        140313          140414                                       "
    };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePLOAD4 (DB_SERVICEFACTORY, dividedEntryInfo);
    std::vector<SG::DataStructure::FEM::PLOAD4Data> Ploads;

    SG::DataStructure::FEM::PLOAD4Data data;
    ASSERT_EQ (DB_SERVICEFACTORY->get<IPLOAD4Service> ()->FindBySId (4, Ploads), SG::DataStructure::Common::Status::STATUS_Success);
}
